package edu.bu.cs673.superkids123.children.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * NumberMatchingGameAnswerProvider
 * 
 * This class provides randomly generated possible answer selections so the
 * matching number game is different every time it is played. Also, keep track
 * of the correct answer for each matching question in a separate map.
 * 
 * @author David Bardwell
 * 
 */
public class NumberMatchingGameAnswerProvider {

	public static final int TOTAL_QUESTION_COUNT = 10;

	private int[] possibleAnswers = new int[TOTAL_QUESTION_COUNT];
	private Map<Integer, int[]> allAnswers = new HashMap<Integer, int[]>();
	private Map<Integer, Integer> correctAnswers = new HashMap<Integer, Integer>();
	private int totalAnswerSlots;

	public NumberMatchingGameAnswerProvider(int totalAnswerSlot) {
		this.totalAnswerSlots = totalAnswerSlot;
	}

	public Map<Integer, int[]> getAnswerSelections() {
		return allAnswers;
	}

	public Map<Integer, Integer> getCorrectAnswers() {
		return correctAnswers;
	}

	/**
	 * generateRandomAnswers
	 * 
	 * Generates random answers for each of the 10 number matching questions.
	 * Two maps are used to store the final answer information.
	 * 
	 */
	public void generateRandomAnswers() {
		int randomNumberIndex = 0;
		for (int currentNumber = 1; currentNumber <= TOTAL_QUESTION_COUNT; currentNumber++) {
			int[] nextAnswers = new int[totalAnswerSlots];
			resetPossibleAnswers(currentNumber);

			for (int answerPosition = 0; answerPosition < totalAnswerSlots - 1; answerPosition++) {
				setRandomNumberAnswer(answerPosition, nextAnswers);
			}
			nextAnswers[totalAnswerSlots - 1] = currentNumber;

			// randomly set correct answer in the nextAnswers array
			Random generator = new Random();
			randomNumberIndex = generator.nextInt(totalAnswerSlots);

			int save = nextAnswers[randomNumberIndex];
			nextAnswers[randomNumberIndex] = currentNumber;
			nextAnswers[totalAnswerSlots - 1] = save;

			allAnswers.put(currentNumber, nextAnswers);
			correctAnswers.put(currentNumber, randomNumberIndex);
		}
	}

	// pick one possible answer randomly and save it in nextAnswers array
	private void setRandomNumberAnswer(int position, int[] nextAnswers) {
		int randomNumberIndex = 0;

		// get range to choose from
		int range = TOTAL_QUESTION_COUNT - 1 - position;

		Random generator = new Random();
		randomNumberIndex = generator.nextInt(range); // 0 to range (exclusive)

		nextAnswers[position] = possibleAnswers[randomNumberIndex];

		// shift down the remaining possible answers to avoid reusing
		// the same number more than once.
		for (int index = randomNumberIndex; index < range - 1; index++) {
			possibleAnswers[index] = possibleAnswers[index + 1];
		}
	}

	private void resetPossibleAnswers(int currentNumber) {
		int index = 0;
		for (int i = 0; i <= TOTAL_QUESTION_COUNT - 1; i++) {
			if (currentNumber != i + 1) {
				possibleAnswers[index] = i + 1;
				index++;
			}
		}
	}
}
